package test;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @author 张开顺
 * @title: FixedLengthFrameDecoder
 * @projectName netty
 * @description: TODO
 * @date 2019/4/1010:06
 */
public class FixedLengthFrameDecoder extends ByteToMessageDecoder {
    private final int frameLength;
    public FixedLengthFrameDecoder(int frameLength){
        this.frameLength=frameLength;
    }

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
//检查是否有足够的字节用于读到下个帧
        if(byteBuf.readableBytes()>=frameLength){
            ByteBuf byteBuf1=byteBuf.readBytes(frameLength);
            list.add(byteBuf1);//添加帧到解码好的消息 List

        }
    }
}
